package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.user.dao.BaseUserResroleMapper;
import com.penghaisoft.microwms.base.web.auth.service.IUserResroleService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseUserResroleKey;
import com.penghaisoft.microwms.common.dto.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by zxcq0 on 2017/3/3.
 * 用户-角色关联操作
 * 
 */
@Service
public class UserResroleServiceImpl implements IUserResroleService {

    @Autowired
    private BaseUserResroleMapper baseUserResroleMapper;

    @Override
    public Resp addResroles(List<BaseUserResroleKey> userResroleKeys) {
        Resp resp = new Resp();
        resp.setCode(Constant.FAIL);
        int count = baseUserResroleMapper.insertBatch(userResroleKeys);
        if (count > 0){
            resp.setCode(Constant.SUCCESS);
        }else {
            resp.setMsg("新增失败");
        }
        return resp;
    }

    @Override
    public Resp deleteResroles(List<BaseUserResroleKey> userResroleKeys) {
        int count = 0;
        for (BaseUserResroleKey userResroleKey : userResroleKeys) {
            count += baseUserResroleMapper.deleteByPrimaryKey(userResroleKey);
        }
        Resp resp = new Resp();
        resp.setCode(Constant.FAIL);
        if (count > 0){
            resp.setCode(Constant.SUCCESS);
        }else {
            resp.setMsg("删除失败");
        }
        return resp;
    }

    /**
     * @Description: 根据传入的userId删除记录--删除用户时使用
     * @author zxcq0
     * @date 2017/3/3 16:07
     * @param
     * @return
     */
    @Override
    public Resp deleteByUserIds(List<String> userIds) {
        Resp resp = new Resp();
        int count = baseUserResroleMapper.deleteByUserIds(userIds);
        resp.setCode(Constant.SUCCESS);
        return resp;
    }

    /**
     * @Description: 根据传入的resroleId删除记录--删除角色时使用
     * @author zxcq0
     * @date 2017/3/3 16:07
     * @param
     * @return
     */
    @Override
    public Resp deleteByResroleIds(List<String> resroleIds) {
        Resp resp = new Resp();
        int count = baseUserResroleMapper.deleteByResroleIds(resroleIds);
        resp.setCode(Constant.SUCCESS);
        return resp;
    }

    /**
     * @Description: 获得某个用户拥有的角色ids
     * @author zxcq0
     * @date 2017/3/6 10:23
     * @param
     * @return
     */
    @Override
    public List<String> queryUserResroleIds(BaseUserResroleKey condition) {
        return baseUserResroleMapper.selectUserResroleIds(condition);
    }
    
    /**
     * @Description: 更新某个用户拥有的角色（用户分配角色，先删除角色关系，再加入角色关系）
     * @author lt
     * @date 2017/3/17 10:00
     * @param
     * @return
     */
    @Override
    @Transactional
    public Resp updateUsrRoleRelation(List<String> userIdList, List<BaseUserResroleKey> list) {
    	Resp resp = new Resp();
        resp.setCode(Constant.FAIL);
        baseUserResroleMapper.deleteByUserIds(userIdList);
        if(!list.isEmpty()){
        	int count = baseUserResroleMapper.insertBatch(list);
            if (count > 0){
                resp.setCode(Constant.SUCCESS);
            }else {
                resp.setMsg("新增失败");
            }
        }else{
        	resp.setCode(Constant.SUCCESS);
        }
        
        return resp;
    }
}
